package Presentacion;


import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;
import AccesoDatos.Comando;
import AccesoDatos.Conexion;
import LogicaNegocio.Empleado;
import LogicaNegocio.Usuario;
import java.awt.Dimension;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author REYES
 */
public class frmAdminEmpleado extends javax.swing.JInternalFrame {

       Conexion conexion = null;
    
    public frmAdminEmpleado(Conexion Pconexion) {
        conexion = Pconexion;
        initComponents();
        tbEmpleados.getColumnModel().getColumn(0).setPreferredWidth(40);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtCedula = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbEmpleados = new javax.swing.JTable();
        btnSalir = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Cédula:");

        txtCedula.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Nombre:");

        txtNombre.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        btnBuscar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/src/imagenes/buscar.png"))); // NOI18N
        btnBuscar.setText("Buscar");

        tbEmpleados.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        DefaultTableModel modelo = new DefaultTableModel();
        modelo.addColumn("Cédula");
        modelo.addColumn("Nombre");
        modelo.addColumn("Primer Apellido");
        modelo.addColumn("Tipo");
        tbEmpleados.setModel(modelo);
        cargarTbEmpleados(modelo);
        tbEmpleados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbEmpleadosMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbEmpleadosMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbEmpleados);

        btnSalir.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/src/imagenes/salir_login.png"))); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/src/imagenes/agregar.png"))); // NOI18N
        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66)
                        .addComponent(btnBuscar)))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void cargarTbEmpleados(DefaultTableModel modelo){  // Carga los empleados existente en la base de datos al JTables
        try{
            Comando comando = new Comando("select Id_Empleado, Nombre, Primer_Apellido, Tipo from tb_empleado");
            ResultSet rs = comando.ejecutarSQL(conexion); 
            while (rs.next())
            {
    // Se crea un array que será una de las filas de la tabla.
            Object [] fila = new Object[4]; // Hay cuatro columnas en la tabla

    // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
            for (int i=0;i<4;i++)
                fila[i] = rs.getObject(i+1); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

    // Se añade al modelo la fila completa.
            modelo.addRow(fila);
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage(), null, JOptionPane.ERROR_MESSAGE);
        }
    }
    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Empleado emp = new Empleado();
        frmAgregarEmpleado fAgregarEmpleado = new frmAgregarEmpleado(conexion, 0, emp);
        Dimension desktopSize = frmMain.jDesktopPane1.getSize();
        Dimension jInternalFrameSize = fAgregarEmpleado.getSize();
        int width = (desktopSize.width - jInternalFrameSize.width) / 2;
        int height = (desktopSize.height - jInternalFrameSize.height) / 2;
        fAgregarEmpleado.setLocation(width, height);
        frmMain.jDesktopPane1.add(fAgregarEmpleado);
        this.dispose();
        fAgregarEmpleado.show();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tbEmpleadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbEmpleadosMouseClicked
        seleccion();
    }//GEN-LAST:event_tbEmpleadosMouseClicked

    private void tbEmpleadosMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbEmpleadosMousePressed
        seleccion();
    }//GEN-LAST:event_tbEmpleadosMousePressed
    
    private void seleccion(){
        try{
            int row=tbEmpleados.getSelectedRow();
            Comando comando = new Comando("select tb_empleado.Id_Empleado, tb_empleado.Nombre, tb_empleado.Primer_Apellido, tb_empleado.Segundo_Apellido, tb_empleado.Telefono, tb_empleado.Email, tb_empleado.Tipo, tb_usuario.Username, tb_usuario.Password, tb_usuario.Tipo from tb_empleado, tb_usuario where tb_empleado.Id_Empleado = '"+ tbEmpleados.getValueAt(row, 0) +"' and tb_usuario.Id_Empleado = '"+ tbEmpleados.getValueAt(row, 0) +"'");
            ResultSet rs = comando.ejecutarSQL(conexion);
            rs.next();
            Empleado emp=new Empleado();
            Usuario user=new Usuario();
            user.setUsername(rs.getString("Username"));
            user.setPassword(rs.getString("Password"));
            emp.setId_Empleado(rs.getInt("Id_Empleado"));
            emp.setNombre(rs.getString("Nombre"));
            emp.setPrimer_Apellido(rs.getString("Primer_Apellido"));
            emp.setSegundo_Apellido(rs.getString("Segundo_Apellido"));
            emp.setTelefono(rs.getString("Telefono"));
            emp.setEmail(rs.getString("Email"));
            emp.setTipo(rs.getString("Tipo"));
            emp.setUsuario(user);
            
            frmAgregarEmpleado fAgregarEmpleado = new frmAgregarEmpleado(conexion, 1, emp); // 1 significa modo: Modificar
            Dimension desktopSize = frmMain.jDesktopPane1.getSize();
            Dimension jInternalFrameSize = fAgregarEmpleado.getSize();
            int width = (desktopSize.width - jInternalFrameSize.width) / 2;
            int height = (desktopSize.height - jInternalFrameSize.height) / 2;
            fAgregarEmpleado.setLocation(width, height);
            frmMain.jDesktopPane1.add(fAgregarEmpleado);
            this.dispose();
            fAgregarEmpleado.show();
        
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage(), null, JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbEmpleados;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
